// pages/travel/travel.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    show:false,
    imgData:[],
    winWth:"",
    actions: [
      {
        name: '纯文本',
      },
      {
        name: '视频和照片',
      },
    ],
    dataList:[],
    popupShow:false,
    commentVal:"",
    commentShow:"true",
    commentList:"",
    commentLoading:false,
    isLoading:"true"
  },
 //获取屏幕尺寸
  getWinWth(){
     var winWth=wx.getSystemInfoSync().screenWidth;    
    this.setData({
      winWth:winWth
    })
  },
  //浮动发布按钮
  floatBtn(){
    if(app.globalData.userInfoName){
      wx.showActionSheet({
        itemList:["文字","图片","视频"],
        success:res=>{
          wx.navigateTo({
            url: '/pages/trSubmit/trSubmit?tapIdx='+res.tapIndex
          })
        }
      })
      this.setData({ show: true });
    }
    else
    {
        wx.navigateTo({
          url: '/pages/login/login',
        })
    }
  },
//获取页面数据
  getpageData(num=0,page=7)//skip,limit
  {
    wx.cloud.callFunction({
      name:"xiangcun_trSubmit_get",
      data:{
        num:num,
        page:page
      }, 
      success:res=>{
       var newDataList=[...this.data.dataList,...res.result]
        if(res.result.length==0||res.result.length<7){
          this.setData({
            isLoading:false
          })
        }
        this.setData({
          dataList:newDataList
        })
      }
  })
},

  //点赞
  click_zan(res){
     if(app.globalData.userInfo)
     {
      wx.showLoading({
        mask:true
      })
      var id = res.currentTarget.dataset.id
      var idx = res.currentTarget.dataset.idx
      wx.cloud.callFunction({
        name: 'xiangcun_trSubmit_zan',
     
        data: {
          id:id,
        },
        success:res=>{
          // console.log(res) 
          var _dataList = this.data.dataList;
          _dataList[idx].isZan = !_dataList[idx].isZan
          var isZan  = this.data.dataList[idx].isZan;
          var zanSize = this.data.dataList[idx].zanSize;
          if(isZan){
            zanSize--;
            _dataList[idx].zanSize = zanSize;
          }
          else
          {
            zanSize++;
            _dataList[idx].zanSize = zanSize;
          }
          this.setData({
            dataList:_dataList
          })
          wx.hideLoading()
        },
        fail: console.error
      })
     }
     else{
       wx.navigateTo({
         url: '/pages/login/login',
       })
     }
   
  },
  //监听评论框是否输入内容
  commentIpt(res){
    if(res.detail){
        this.setData({
          commentShow:false, 
         commentVal:res.detail
       })
    }
    else
    {
      this.setData({
        commentShow:true,
    
     })
    }
  }, 

//删除发布的帖子
    clickDel(e){
      var id = e.currentTarget.dataset.id
      var index = e.currentTarget.dataset.index
      wx.showModal({
        title:"是否确认删除",
        success:res=>{
          if(res.confirm)
          {
            wx.showLoading({
              mask:true
            })
            wx.cloud.callFunction({
              name: "xiangcun_trSubmit_del",
              data:{
                id
              },
              success:res=>{
                var _dataList = this.data.dataList
                _dataList.splice(index,1)
                this.setData({
                  dataList:_dataList
                })
                wx.hideLoading()
              }
            })
          }
        }
      })
    },
//点击评论按钮
  click_comment(res){
     if(app.globalData.userInfo)
     {
        var id = res? res.currentTarget.dataset.id : this.data.commentid
        var idx = res? res.currentTarget.dataset.idx : this.data.idx
     this.setData({
        commentShow:true,
        popupShow:true,
        commentid:id,
        idx:idx,
        commentList:"",
        commentLoading:false
      })
      //获取已经存在的评论
      wx.cloud.callFunction({
        name:"xiangcun_trSubmit_comment_show",
        data:{
          commentid:id
        },
        success:res=>{   
          this.setData({
            commentList : res.result.data,
            commentLoading:true
          })
        }
      })
     }
     else
     {
       wx.navigateTo({
         url: '/pages/login/login',
       })
     }   
  },
  //关闭评论
  closeCommond(){
    this.setData({
      popupShow:false
     })
  },
  //确认发布评论
  clickBtnComment(){
    wx.showLoading({
      mask:true
    })
    var content= this.data.commentVal
    var userInfo = app.globalData.userInfo
    var commentid=this.data.commentid
    var username = app.globalData.userInfoName
    wx.cloud.callFunction({
      name:"xiangcun_trSubmit_comment_add",
      data:{
        content,
        userInfo,
        commentid,
        username
      },
      success:res=>{
        if(res.result.errCode==87014)
            {
              wx.showToast({
                title: '发布内容不符合规范!',
                icon:"none"
              })
              return ;
            }
        var _dataList = this.data.dataList
        var idx =this.data.idx;
        var commNum = _dataList[idx].commNum
        commNum++;
        _dataList[idx].commNum = commNum
        this.setData({
          commentVal:"",
          dataList :_dataList
        })
        this.click_comment();
       wx.hideLoading()
      }
    })
  },
  
 
  //查看大图
  checkImage(res){
    // console.log(res)
    wx.previewImage({
      urls:res.currentTarget.dataset.urls,
      current: res.currentTarget.dataset.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({
        title: '数据加载中',
        mask:true
      },1000)
        this.getWinWth()
        this.getpageData();
        // this.getpageData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      loading: false,
    });
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var num=this.data.dataList.length;
    this.getpageData(num);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})